package
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	//---------------------------------------------------------------------------//
	public class Grid extends Sprite
	{
		public var squares:Array;
		
		//-----------------------------------------------------------------------//
		public function Grid()
		{
			super();
			squares = new Array();
			var newShape:Shape;
			var currentSquare:int;
			const squareWidth:Number = 75;
			const spacing:Number = 1;
			for (var i:int =0 ;i<5;i++)
			{
				for (var j:int=0;j<5;j++)
				{
					currentSquare = i*5+j;
					newShape = new Shape();
					newShape.graphics.lineStyle(1);
					if (currentSquare%2 == 0 )
						newShape.graphics.beginFill(0xFF0000,1);
					else
						newShape.graphics.beginFill(0x0000FF,1);
					newShape.x = squareWidth*0.5 * j;
					newShape.y = squareWidth*0.5 * i;
					newShape.graphics.drawRect(newShape.x,newShape.y,squareWidth,squareWidth);
					squares[currentSquare] = newShape;
										
					this.addChild(newShape);
					
				}
			}
			
			this.addEventListener(MouseEvent.CLICK,OnGridClick);
			
		}
		//-----------------------------------------------------------------------//
		public function FindSquare(posX:Number,posY:Number):Shape
		{
			var shapeToReturn:Shape = null;
			for each (var shap:Shape in squares)
			{
				if (shap.hitTestPoint(posX,posY))
				{
					shapeToReturn=shap;
				}
			}
			return shapeToReturn;
		}
		//-----------------------------------------------------------------------//
		public function OnGridClick(ev:MouseEvent):void
		{
			var square:Shape = FindSquare(ev.localX,ev.localY);		
			if (square)
				trace (squares.indexOf(square));
							
		}
		//-----------------------------------------------------------------------//
		public function OnMouseDown(ev:MouseEvent):void
		{
			var square:Shape = FindSquare(ev.localX,ev.localY);
			if (square)
				square.visible = false;
		}
		//-----------------------------------------------------------------------//
	}
	//---------------------------------------------------------------------------//
}